﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using nrtech.bot.compras.bll;

namespace nrtech.bot.compras.ui
{
    public partial class fConfig : Form
    {
        public fConfig()
        {
            InitializeComponent();
        }

        private void populateUsers()
        {
            try
            {
                cbUser.DataSource = Config.getUsers().Tables[0];
                cbUser.ValueMember = "userSite";
                cbUser.DisplayMember = "userSite";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao carregar configurações: " + ex.Message, "Erro");
            }
        }

        private void fConfig_Load(object sender, EventArgs e)
        {
            populateUsers();
        }

        private void carregaInformacoes()
        {
            try
            {
                Config.lerConfig(cbUser.SelectedValue.ToString());
                tbHoraMonitor.Text = DateTime.Parse(Config.horaMonitor).ToString("HH:mm");
                tbTags.Text = Config.tags;
                tbPassSite.Text = Config.passSite;
                tbEmail.Text = Config.email;
                tbSmtp.Text = Config.smtp;
                tbUser.Text = Config.userMail;
                tbSenha.Text = Config.passMail;
                tbPorta.Text = Config.porta.ToString();
                cbReenviar.Checked = Config.reenvia;
                cbSegura.Checked = Config.ssl;
                tbIntervalo.Text = Config.intervalo.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                Config.horaMonitor = tbHoraMonitor.Text + ":00";
                Config.tags = tbTags.Text;
                Config.userSite = cbUser.SelectedValue.ToString();
                Config.passSite = tbPassSite.Text;
                Config.email = tbEmail.Text;
                Config.smtp = tbSmtp.Text;
                Config.userMail = tbUser.Text;
                Config.passMail = tbSenha.Text;
                Config.porta = Convert.ToInt32(tbPorta.Text);
                Config.reenvia = cbReenviar.Checked;
                Config.ssl = cbSegura.Checked;
                Config.intervalo = Convert.ToInt32(tbIntervalo.Text);

                DateTime horaExec = Convert.ToDateTime(tbHoraMonitor.Text);
                horaExec = horaExec.AddMinutes(Config.intervalo * (-1));
                Config.ultimoMonitor = horaExec.Hour.ToString() + ":" + horaExec.Minute.ToString();

                Config.salvaConfigGeral();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao salvar configurações: " + ex.Message, "Erro");
            }
        }

        private void cbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            carregaInformacoes();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String nomeNovoUser = "";
            if (InputBox.show("Novo usuário", "Nome de logon", ref nomeNovoUser) == DialogResult.OK)
            {
                Config.tags = "";
                Config.userSite = nomeNovoUser;
                Config.passSite = "";
                Config.insereConfig();

                populateUsers();
                cbUser.SelectedValue = nomeNovoUser;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Deseja mesmo excluir este usuário?", "Confirmação", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Config.excluirConfig(cbUser.SelectedValue.ToString());
            }
        }
    }
}
